{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ROI区域"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 位置提取ROI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 #opencv的缩写为cv2\n",
    "import matplotlib.pyplot as plt # matplotlib库用于绘图展示\n",
    "import numpy as np   # numpy数值计算工具包\n",
    "\n",
    "# 魔法指令，直接展示图，Jupyter notebook特有\n",
    "%matplotlib inline   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cv_show(name,img):\n",
    "    cv2.imshow(name,img)\n",
    "    cv2.waitKey(0)\n",
    "    cv2.destroyAllWindows()\n",
    "\n",
    "img = cv2.imread('01_Picture/01_cat.jpg')\n",
    "cat = img[0:200,0:200] # 选择图片感兴趣的区域\n",
    "cv_show('cat',cat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 通道提取ROI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1 分离 BGR 通道"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b.shape: (414, 500)\n",
      "g.shape: (414, 500)\n",
      "r.shape: (414, 500)\n",
      "img.shape: (414, 500, 3)\n"
     ]
    }
   ],
   "source": [
    "img = cv2.imread('01_Picture/01_cat.jpg')\n",
    "b,g,r = cv2.split(img)\n",
    "cv_show('cat_b',b)\n",
    "print('b.shape:',b.shape) # B通道，单通道，灰度图\n",
    "cv_show('cat_g',g)\n",
    "print('g.shape:',g.shape) # G通道，单通道，灰度图\n",
    "cv_show('cat_r',r)\n",
    "print('r.shape:',r.shape) # R通道，单通道，灰度图\n",
    "img = cv2.merge((b,g,r))\n",
    "print('img.shape:',img.shape) # 3 通道，彩色图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2 展示 R 通道"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 只保留 R\n",
    "img = cv2.imread('01_Picture/01_cat.jpg')\n",
    "b,g,r = cv2.split(img)\n",
    "img = cv2.merge((b,g,r))\n",
    "cur_img = img.copy()\n",
    "cur_img[:,:,0] = 0 \n",
    "cur_img[:,:,1] = 0\n",
    "cv_show('R',cur_img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.3 展示 G 通道"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 只保留 G\n",
    "img = cv2.imread('01_Picture/01_cat.jpg')\n",
    "cur_img = img.copy()\n",
    "cur_img[:,:,0] = 0 \n",
    "cur_img[:,:,2] = 0\n",
    "cv_show('G',cur_img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.4 展示 R 通道"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 只保留 R\n",
    "img = cv2.imread('01_Picture/01_cat.jpg')\n",
    "cur_img = img.copy()\n",
    "cur_img[:,:,1] = 0 \n",
    "cur_img[:,:,2] = 0\n",
    "cv_show('B',cur_img)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
